<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${sysConfig == null ? '发布' : '编辑'}系统参数</title>
    <%@include file="../../include/commonFile.jsp" %>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
</head>
<body>
<!--头部-->
<%@include file="../../include/header.jsp" %>
<div class="index-outside">
    <%@include file="../../include/sidebar.jsp" %>
    <!--内容-->
    <section>
        <div class="section-main">

            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/notify/event/save.do">
                    <input type="hidden" name="id" value="${sysConfig.id}"/>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">参数名称<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" lay-verify="name" autocomplete="off" class="layui-input"
                                       value="${sysConfig.name}"
                                >
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">参数代码<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="code" lay-verify="code" autocomplete="off" class="layui-input"
                                       value="${sysConfig.code}"
                                >
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">排序<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <input type="text" name="sort" lay-verify="sort" autocomplete="off" class="layui-input"
                                       value="${sysConfig.sort}"
                                >
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">类型<span class="f-verify-red">*</span></label>
                            <div class="layui-input-inline">
                                <select name="type" lay-verify="type" lay-filter="type">
                                    <option value="">请选择类型</option>
                                    <c:forEach var="type" items="${types}">
                                        <option value="${type.key}" ${sysConfig.type == type.key ? 'selected="selected"' : ""}>${type.value}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item switchDiv" ${sysConfig.type == 0 ? '' : 'style="display:none"'}>
                        <label class="layui-form-label">参数值<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <input type="radio" name="paramRadio" value="true" lay-filter="paramRadio"
                                   title="开" ${sysConfig == null || sysConfig.value == "true" ? 'checked="checked"' : ''} />
                            <input type="radio" name="paramRadio" value="false" lay-filter="paramRadio"
                                   title="关" ${sysConfig.value == "false" ? 'checked="checked"' : ''} />
                        </div>
                    </div>
                    <div class="layui-form-item paramDiv" ${sysConfig.type == 1 ? '' : 'style="display:none"'}>
                        <label class="layui-form-label">参数值<span class="f-verify-red">*</span></label>
                        <div class="layui-input-block">
                            <textarea name="paramText" placeholder="参数值"
                                      class="layui-textarea">${sysConfig.value}</textarea>
                        </div>
                    </div>
                    <input type="hidden" name="value" value="${sysConfig.value}" lay-verify="value"/>
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <input type="text" name="remarks" autocomplete="off" class="layui-input"
                                   value="${sysConfig.remarks}"
                            >
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit lay-filter="*">立即提交</a>
                            <a href="${ctx}/system/config/list.do" class="layui-btn layui-btn-primary">取消</a>
                        </div>
                    </div>
                </form>
            </div>
        </div><!--底部-->
        <%@include file="../../include/footer.jsp" %>
    </section>
</div>

<script>
    $(function () {
        var typeRadioValue = '${sysConfig.type}' || null;
        layui.use(['form', 'laydate'], function () {
            var form = layui.form;

            //自定义验证规则
            form.verify({
                name: function (value) {
                    if (value == "") {
                        return '请填写通道名称';
                    }
                },
                code: function (value) {
                    if (value == "") {
                        return "请填写通道代码";
                    } else {
                        var isRepeat = ajaxSubmit({
                            configId: '${sysConfig.id}',
                            code: value
                        }, '${ctx}/system/config/checkCodeUnique.do');
                        if (isRepeat == false) {
                            return 'code已存在';
                        }
                    }
                },
                sort: function (value) {
                    if (value == "") {
                        return "请填排序值";
                    }
                },
                value: function (value) {
                    if (value == "") {
                        return "请填写参数值";
                    }
                }
            });

            //监听提交
            form.on('submit', function (data) {
                txz.submitObject(data.elem, function (callBack) {
                    $.post('${ctx}/system/config/save.do', $('#myForm').serialize(), function (res) {
                        typeof callBack === 'function' && callBack();
                        if (res.success) {
                            util.layerMsgSuccess("提交成功", function () {
                                location.href = "${ctx}/system/config/list.do";
                            });
                        } else {
                            util.layerMsgError(res.description)
                        }
                    });
                })
                return false;
            });

            form.on('select(type)', function (data) {
                typeRadioValue = data.value;
                if (typeRadioValue == 0) {
                    $(".switchDiv").show();
                    $(".paramDiv").hide();
                } else if (typeRadioValue == 1) {
                    $(".switchDiv").hide();
                    $(".paramDiv").show();
                }
            })

            form.on('radio(paramRadio)', function (data) {
                $("[name=value]").val(data.value);
            });

            $("[name=paramText]").bind('input propertychange blur', function (e) {
                $("[name=value]").val($(this).val());
            });
        });
    })

    function ajaxSubmit(data, url) {
        var isRepeat = false;
        $.ajax({
            type: 'POST',
            async: false, // 使用同步的方法
            data: data,
            dataType: 'json',
            success: function (result) {
                isRepeat = result;
            },
            url: url
        });
        return isRepeat;
    }


</script>
<script type="text/javascript">
    txz.initHeader({
        nav: [{
            name: '配置管理',
            href: '${ctx}/system/config/list.do'
        }, {
            name: '${sysConfig == null ? '发布' : '编辑'}系统参数',
            curr: true
        }],
        btns: [{
            name: '返回',
            href: 'javascript:history.back();',
            icon: 'back'
        }]
    });
</script>
</body>
</html>